package edu.mobbuzz.web.components.rss;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.output.NullWriter;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput;

public class RssFeedWriter {
	/*@Inject( "infrastructure:markupWriterSource" )
    public abstract MarkupWriterSource getMarkupWriterSource(); 
    
    @Parameter( defaultValue = "literal:rss_2.0" )
    public abstract String getFeedFormat();
    
    @Parameter( required = true )
    public abstract String getTitle();
    
    @Parameter( required = true )
    public abstract String getLink();
    
    @Parameter( required = true )
    public abstract String getDescription();
    
    @Parameter
    public abstract Date getPublishDate();
    
    @Property
    public abstract SyndFeed getFeed();
    public abstract void setFeed( SyndFeed feed );
    
    public static final String RSS_FEED_ATTRIBUTE = "rssFeed"; 
    
    @Property
    public abstract List< SyndEntry > getEntryList();
    public abstract void setEntryList( List< SyndEntry > entryList );
    
    @Override
    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
    {
            cycle.setAttribute( RSS_FEED_ATTRIBUTE, this );
            try 
            {
                    SyndFeed feed = new SyndFeedImpl();
                    feed.setFeedType( getFeedFormat() );
                    feed.setTitle( getTitle() );
                    feed.setLink( getLink() );
                    feed.setDescription( getDescription() );
                    feed.setPublishedDate( getPublishDate() );
                    
                    setEntryList( new ArrayList< SyndEntry >() );

                    renderBody( new NullWriter(), cycle );
                    
                    cycle.setAttribute( RSS_FEED_ATTRIBUTE, null );
                    
                    feed.setEntries( getEntryList() );
                    
                    SyndFeedOutput output = new SyndFeedOutput();
                    writer.printRaw( output.outputString( feed ) );
            }
            catch ( Exception ex )
            {
                    throw new RuntimeException( ex );
            }
    }
    
    public void addEntry( SyndEntry entry )
    {
            getEntryList().add( entry );
    }
*/
}
